<script>
	var client;
	export default {
		onLaunch: function() {
			console.log('App Launch')
			const updateManager = uni.getUpdateManager();
			updateManager.onCheckForUpdate(res => {
				console.log(res);
			})
			updateManager.onUpdateReady(function () {
			  unis.showModal({
			    title: '更新提示',
			    content: '新版本已经准备好，是否重启应用？',
			    success: function (res) {
			      if (res.confirm) {
			        // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
			        updateManager.applyUpdate()
			      }else if(res.cancel){
							supdateManager.applyUpdate()
						}	
			    } 
			  })
			})
			
			updateManager.onUpdateFailed(function () {
			  // 新版本下载失败
			  uni.showToast({
				  title: '更新失败',
				  icon: 'none',
			  })
			})
			this.connect();
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods:{
			connect(){
				var mqtt = require('./common/mqtt.min.js')
				var options={
					keepalive: 60, //60s
					connectTimeout: 40000,
					clientId:Math.random().toString(36).substr(2),
					username: '小程序',
					// password: MQTT_PASSWORD,
					clean: true
				};
				// #ifdef H5
				client = mqtt.connect('ws://42.192.121.180:8083/mqtt',options)
				// #endif
				// #ifdef MP-WEIXIN||APP-PLUS
				client = mqtt.connect('wx://42.192.121.180:8083/mqtt',options)
				// #endif
				client.on('connect',(e)=>{
					console.log('成功连接服务器')
					client.subscribe('/test/post',{qos:2},err=>{
						if(!err){
							console.log("订阅成功")
						}
					})
					client.subscribe('/test/post1',{qos:2},err=>{
						if(!err){
							console.log("订阅成功")
						}
					})
					client.subscribe('/test/post2',{qos:2},err=>{
						if(!err){
							console.log("订阅成功")
						}
					})
					client.subscribe('/test/post3',{qos:2},err=>{
						if(!err){
							console.log("订阅成功")
						}
					})
					client.publish('/test/post','订阅成功111')
				}).on('reconnect',()=>{
					console.log('重新连接服务器')
				}).on('error',()=>{
					console.log('error')
				}).on('end',()=>{
					console.log('end')
				}).on('message',(topic,message)=>{
					console.log('message:'+message)
				})
			},
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import "uview-ui/index.scss";
</style>
